home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Trading on the Edge
/
Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin
/
pc
/
mac_file
/
vendor_d
/
neuralwa
/
nw2v50
/
pattern.c
< prev
next >
Wrap
Text File
|
1993-08-23
|
29KB
|
951 lines
/*09-Feb-89 (pattern.c) Pattern I/O routine */
/* 13Apr90 -emb -chgs for Zortech v2.0 compatibility */
/************************************************************************
* Copyright(C) 1987-1992 NeuralWare Inc *
* Penn Center West, IV-227, Pittsburgh, PA 15276 *
* Telephone: (412) 787-8222 FAX: (412) 787-8220 *
* *
* All rights reserved. No part of this program may be reproduced, *
* stored in a retrieval system, or transmitted, in any form or by any *
* means, electronic, mechanical, photocopying, recording or otherwise *
* without the prior written permission of the copyright owner, *
* NeuralWare, Inc. *
* *
* PROPRIETARY NOTICE *
* *
* This document is the property of NeuralWare, Inc. and contains *
* trade-secrets and other proprietary information. The information *
* herein is reserved as proprietary to NeuralWare, and is not to be *
* published, reproduced, copied, disclosed, used, or reverse *
* engineered without the express written consent of a duly authorized *
* representative of NeuralWare. *
************************************************************************
*/
#include "userutl.h"
#include <string.h>
#ifndef SUN
#ifndef DLC
#include <stdlib.h>
#endif
#endif
#ifdef MAC
#include "macuio.redef"
#endif
/************************************************************************
*
* This is a general purpose pattern I/O routine for use with
* any network type.
*
* Options:
*
* ATTENTION I/O: This allows the user to create a file of
* character patterns and their associated
* ASCII code. This file is written in .nna
* format and can be used either for file I/O
* or for userio learning. The user
* is prompted for file name (.nna extension
* assumed), and is given the option of writing or
* appending the file. The user is provided with
* a grid in which to draw patterns. On entering
* a pattern, the user is prompted to enter, from
* the keyboard, the character that the pattern
* depicts. The file is closed on quitting.
*
* REWIND: If a file is open it is rewound.
*
************************************************************************
*/
/* grid definitions */
static long n_across[2], n_down[2], n_inputs[2], tot_n_inputs;
#define N_ACR_MIN 1
#define N_ACR_MAX 32
#define N_ACC_DEF 6
#define N_DWN_MIN 1
#define N_DWN_MAX 32
#define N_DWN_DEF 8
/* window coordinates - initialized in usrIO( ) */
static int wxstrt[4], wystrt[4], wxend[4], wyend[4];
typedef struct _grid {
unsigned char gr_key; /* window key for grid */
unsigned char gr_flag; /* grid flag */
int gr_xoff, gr_yoff; /* x,y offset from window origin */
int gr_nx, gr_ny; /* # pixels in x, y directions */
int gr_pxx, gr_pxy; /* size of a grid pixel */
int gr_color[3]; /* color of lines and pixels */
#define GR_OFF 0 /* off color index */
#define GR_ON 1 /* on color index */
#define GR_LINE 2 /* line color index */
float *gr_data; /* data for grid */
} GRID;
static GRID ip_grid[2] = { 0 }; /* Input Grids */
/* Local menu definitions */
#define CR_SCROLL 0
#define CR_SEEK 1
#define CR_ENTER 2
#define CR_QUIT 3
#define CR_AUTO 4
#define NULL_STR 0
static GMENU_ITEM PMenuList[] = { /* Menu list */
{ CR_SCROLL, NULL_STR, NULL_STR },
{ CR_SEEK, NULL_STR, NULL_STR },
{ CR_ENTER, NULL_STR, NULL_STR },
{ CR_QUIT, NULL_STR, NULL_STR },
{ CR_AUTO, NULL_STR, NULL_STR }
};
static GMENU AIOMenu = { /* Attention I/O menu */
0,
4, /* # items */
3, /* key */
0x0000
};
static GMENU LrnMenu = { /* Attention I/O menu */
0,
5, /* # items */
3, /* key */
0x0000
};
/* File definitions */
static char filename[13];
static char filenroot[9];
static int curr_pattern, n_patterns; /* pattern index, # patterns */
#define MAX_N_PATTERNS 200
static long *filepos; /* Array of file positions */
static int *shuffle_array;/* Index into shuffle array */
static int shufflex = 0; /* Shuffle Index */
#define MAX_STR 78 /* For reading from file */
static char fbuf[MAX_STR + 1]; /* buffer for file i/o */
static FILE *fp = 0; /* File pointer */
static float *fdata; /* file data */
/* Network parameters */
static int nlayp, ninp, noutp, ltype; /* Network parameters */
static char *csp, *netnp; /* Network pointers*/
static int BamFlag = 0; /* Flags BAM control strat */
/* Graphics parameters */
static int xsize, ysize, ncolor, chrx, chry; /* graphics parameters */
/* Miscellaneous */
static int abortio;
static int autoip;
#if UNIX || MAC || NEXT
strlwr( dp )
char *dp;
{
int c;
for( ; (c = *dp) != 0; dp++ ) {
if ( 'A' <= c && c <= 'Z' ) *dp = c - 'A' + 'a';
}
}
#endif
#ifdef DLC
/* --- prototypes --- */
SetUpGrids( );
DispGrid( GRID * );
VOID DispPattern( GRID * ); /* changed type to VOID;4-28-93 mjs */
VOID Sketch( GRID * ); /* changed type to VOID;4-28-93 mjs */
DrawPixel( GRID *, int, int, int );
FillPosArr( );
ReadFData( );
WriteFData( );
Shuffle( int );
#endif
SetUpGrids( ) /* Set grids and grid window positions */
{
int gridwidth[2];
int inbetween;
int wx;
if ( BamFlag ) inbetween = 20;
else inbetween = 0;
gridwidth[0] = gridwidth[1] = 0;
/* Set up grid parameters */
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
ip_grid[wx].gr_data = wx ? &fdata[n_inputs[0]] : fdata;
ip_grid[wx].gr_key = wx+1;
ip_grid[wx].gr_flag = 0;
ip_grid[wx].gr_xoff = ip_grid[wx].gr_yoff = 0;
ip_grid[wx].gr_nx = n_across[wx];
ip_grid[wx].gr_ny = n_down[wx];
/* Bigger grid => smaller pixels: */
ip_grid[wx].gr_pxx = 20 - ( n_across[wx] + n_down[wx] ) / 4;
if ( ip_grid[wx].gr_pxx < 1 ) ip_grid[wx].gr_pxx = 1;
ip_grid[wx].gr_pxy = ip_grid[wx].gr_pxx;
gridwidth[wx] =
ip_grid[wx].gr_nx * (ip_grid[wx].gr_pxx+1) + 1;
}
/* Now calculate positions of grid windows */
wxstrt[0] =
( xsize - gridwidth[0] - gridwidth[1] - inbetween ) / 2 ;
wystrt[0] = ( ysize - (ip_grid[0].gr_ny * (ip_grid[0].gr_pxy+1) + 1) ) / 2 ;
if (BamFlag) {
wxstrt[1] = wxstrt[0] + gridwidth[0] + inbetween;
wystrt[1] = wystrt[0];
}
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
wxend[wx] = wxstrt[wx] + gridwidth[wx] - 1;
wyend[wx] = wystrt[wx] + ip_grid[wx].gr_ny * (ip_grid[wx].gr_pxy+1);
ug_window( ip_grid[wx].gr_key, ip_grid[wx].gr_color[GR_OFF],
wxstrt[wx], wystrt[wx], wxend[wx], wyend[wx] );
}
}
DispGrid( gridp )
GRID *gridp; /* pointer to grid structure */
{
int wx; /* work index */
int x0,y0,x1,y1; /* endpoints of gridlines */
/* Draw vertical gridlines */
x1 = x0 = gridp->gr_xoff;
y0 = gridp->gr_yoff;
y1 = y0 + gridp->gr_ny * ( gridp->gr_pxy + 1 );
for (wx = 0; wx <= gridp->gr_nx; wx++, x1 = x0 += gridp->gr_pxx + 1 )
ug_line( gridp->gr_key, gridp->gr_color[GR_LINE], 0, x0, y0, x1, y1, 0 );
/* Draw horizontal gridlines */
x0 = gridp->gr_xoff;
x1 = x0 + gridp->gr_nx * ( gridp->gr_pxx + 1 );
y1 = y0 = gridp->gr_yoff;
for (wx = 0; wx <= gridp->gr_ny; wx++, y1 = y0 += gridp->gr_pxy + 1 )
ug_line( gridp->gr_key, gridp->gr_color[GR_LINE], 0, x0, y0, x1, y1, 0 );
}
/* changed type to VOID; 4-28-93 mjs */
VOID DispPattern( gridp )
GRID *gridp; /* Pointer to grid structure */
{
int wx, wy, wz; /* work indices */
float *dp; /* pointer to data */
dp = &gridp->gr_data[0];
if ( dp == ( float * )0 ) return;
for ( wy = gridp->gr_ny, wz = 0; --wy >= 0 ; )
for ( wx = 0; wx < gridp->gr_nx; wx++, wz++ )
DrawPixel( gridp, wx, wy,
gridp->gr_color[ dp[wz]>0.0001 ? GR_ON : GR_OFF ] );
}
/* changed type to VOID; 4-28-93 mjs */
VOID Sketch( gridp )
GRID *gridp; /* Pointer to grid structure */
{
int x0, y0, x1, y1; /* Grid boundary */
int wx, wy, wz; /* work indices */
int key, xp, yp, button; /* mouse parameters */
float *dp; /* pointer to data */
dp = &gridp->gr_data[0];
if ( dp == ( float * )0 ) return;
x0 = gridp->gr_xoff;
y0 = gridp->gr_yoff;
x1 = x0 + gridp->gr_nx * ( gridp->gr_pxx + 1 );
y1 = y0 + gridp->gr_ny * ( gridp->gr_pxy + 1 );
for (;;) {
ug_mouse( &key, &xp, &yp, &button );
if ( key != gridp->gr_key || xp < x0 || xp >= x1 || yp < y0 || yp >= y1 )
return;
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
/* Calculate grid coordinate */
wx = ( xp - x0 ) / ( gridp->gr_pxx + 1 );
wy = ( yp - y0 ) / ( gridp->gr_pxy + 1 );
DrawPixel( gridp, wx, wy,
gridp->gr_color[ button == MBUT_LEFT ? GR_ON : GR_OFF ] );
/* Modify data array */
wz = (gridp->gr_ny - wy - 1) * gridp->gr_nx + wx;
dp[wz] = button == MBUT_LEFT ? 1.0 : -1.0;
}
}
}
DrawPixel( gridp, x, y, color )
GRID *gridp; /* pointer to grid structure */
int x, y; /* grid coordinate */
int color; /* pixel color */
{
int xstrt, ystrt;
xstrt = gridp->gr_xoff + 1 + ( gridp->gr_pxx + 1 ) * x;
ystrt = gridp->gr_yoff + 1 + ( gridp->gr_pxy + 1 ) * y;
ug_boxf( gridp->gr_key, color, 0, xstrt, ystrt,
xstrt + gridp->gr_pxx - 1, ystrt + gridp->gr_pxy - 1 );
}
/* NOTE: following routine will not cope with
general format ".nna" files. In particular, it
is assumed that any new line which does not
start with a continuation character ('&') or
a comment character ('!') is the start of a
non-empty record; it is also assumed that there
are no comments in the middle of records */
FillPosArr( )
{
int wx; /* work index */
int ch; /* Character */
long fpos; /* File position */
int ftt; /* First time through */
fseek( fp, 0l, 0 ); /* beginning of file */
for ( wx = 0; wx < MAX_N_PATTERNS; ) {
if ( (ch=fgetc( fp )) != '!' && ch != '&' ) {
ungetc( ch, fp );
fpos = ftell( fp );
ftt = 1;
} else ftt = 0;
/* Make sure there is something on the line;
scan to end of line */
while ( (ch=fgetc( fp)) != NEW_LINE ) {
if ( ftt && ch >= (int)'0' ) {
filepos[wx++] = fpos;
ftt = 0;
}
if ( ch == EOF ) return( wx );
}
}
}
ReadFData( flag )
int flag; /* Whether or not desired output should be read */
{
int wx, wy; /* work index */
int ch;
float *dp; /* data pointer */
if ( curr_pattern < 0 ) curr_pattern = 0;
if ( curr_pattern >= n_patterns ) { /* Don't read from file */
for ( wx = 0; wx < tot_n_inputs; wx++ )
fdata[wx] = -1.0;
curr_pattern = n_patterns;
} else {
fseek( fp, filepos[curr_pattern], 0 );
/* NOTE: Following code assumes data has been written using
format of WriteFData( ) routine */
for (wx = 0, wy = 18, dp = fdata; wx < tot_n_inputs;
wx++, wy--, dp++ ) {
if ( wy == 0 ) { /* Look for continuation */
while ( (ch=fgetc( fp)) != NEW_LINE )
if ( ch == EOF ) goto bad_return;
if ( fgetc( fp ) != '&' ) goto bad_return;
wy = 18;
}
if ( fscanf( fp, "%f", dp ) <= 0 ) goto bad_return;
}
}
return( 0 );
bad_return:
PutStr( "Bad file format\n" );
return( -1 );
}
WriteFData( flag )
int flag; /* Whether or not desired output should be written */
{
int wx, wy; /* work indices */
if ( curr_pattern < 0 ) curr_pattern = 0;
if ( curr_pattern >= n_patterns ) {
curr_pattern = n_patterns;
n_patterns++;
fseek( fp, 0l, 2 ); /* Seek End of File */
filepos[curr_pattern] = ftell( fp );
}
fseek( fp, filepos[curr_pattern], 0 );
fputc( ' ', fp );
for ( wx = 0, wy = 18; wx < tot_n_inputs; wx++, wy-- ) {
if ( wy == 0 ) {
fputc( NEW_LINE, fp );
fputc( '&' , fp ); /* Continuation line */
wy = 18;
}
fprintf( fp, " %2.0f.", fdata[wx] );
}
fputc( NEW_LINE, fp );
curr_pattern++;
}
Shuffle( size )
int size;
{
int wx, wy, wz;
int swap;
for ( wx = 0, wy = size; wx < size; wx++, wy-- ) {
wz = rand( ) % wy;
swap = shuffle_array[wx];
shuffle_array[wx] = shuffle_array[wx + wz];
shuffle_array[wx + wz] = swap;
}
}
/************************************************************************
*
* UsrIO - user I/O routine to handle requests from NWORKS
*
************************************************************************
*/
static int InitFlag = 0; /* initialize things flag */
void UsrIO() /* handle NWORKS requests */
{
int key, xp, yp, button; /* mouse interface */
int newfile;
int wx; /* work index */
GMENU_ITEM *gmip;
char sbuf[60], *sp; /* work string and pointer */
if ( InitFlag == 0 ) {
AIOMenu.item = PMenuList;
LrnMenu.item = PMenuList;
PMenuList[0].text = "Scroll";
PMenuList[1].text = "Rewind";
PMenuList[2].text = "Enter";
PMenuList[3].text = "Quit";
PMenuList[4].text = "Auto";
if ((filepos=(long *)malloc(MAX_N_PATTERNS*sizeof(long)))
== (long *)0 ||
(shuffle_array=(int *)malloc(MAX_N_PATTERNS*sizeof(int)))
== (int *)0 ||
(fdata=(float *)malloc(2*N_ACR_MAX*N_DWN_MAX*sizeof(float)))
== (float *)0 ) {
PutStr("Not Enough memory to run pattern\n");
IORTNCDE = -1;
return;
}
/* Get screen parameters */
ug_gparms( &xsize, &ysize, &ncolor, &chrx, &chry);
if ( ncolor < 8 ) {
gm_intcolor = 1;
gm_txtcolor = 0;
gm_outcolor = 0;
} else {
gm_intcolor = 7;
gm_txtcolor = 4;
gm_outcolor = 0;
}
for ( wx=0; wx<2; wx++ ) {
ip_grid[wx].gr_color[GR_OFF] = gm_intcolor;
ip_grid[wx].gr_color[GR_ON] = gm_txtcolor;
ip_grid[wx].gr_color[GR_LINE] = gm_txtcolor;
}
InitGMenu( &AIOMenu, chrx, chry );
AIOMenu.x0 = 0; /* Menu position relative to window */
AIOMenu.y0 = 0;
InitGMenu( &LrnMenu, chrx, chry );
LrnMenu.x0 = 0; /* Menu position relative to window */
LrnMenu.y0 = 0;
ug_rdnetinf( &nlayp, &ninp, &noutp, <ype, &csp, &netnp );
strncpy( filenroot, netnp, 8 ); /* Read network name */
filenroot[8] = '\0';
strncpy( sbuf, csp, 3 ); /* Get first 3 letters of control strategy */
sbuf[3] = '\0';
strlwr( sbuf ); /* convert to lower case */
/* See if BAM control strategy - this will require 2 input grids */
if ( strcmp( sbuf, "bam" ) == 0 ) BamFlag = 1;
else BamFlag = 0;
InitFlag = 1;
}
IORTNCDE = 0; /* good return for data */
switch( IOREQCDE ) {
case RQ_ATTENTION:
if ( fp != (FILE *) 0 ) fclose( fp );
sprintf( sbuf,
"Enter name of pattern file ( default: %s ):", filenroot );
PutStr( sbuf );
sp = GetStr( );
PutStr("\n");
if ( strcmp( sp, "" ) != 0 ) strncpy( filenroot, sp, 9 );
strcpy( filename, filenroot );
strcat( filename, ".nna" );
if ( (fp = fopen( filename, "r" )) == (FILE *)0 )
{
newfile = 1;
}
else
{
newfile = 0;
fclose(fp);
}
if ( ( fp = fopen( filename, "a+" ) ) == (FILE *)0 ) {
sprintf( sbuf, "Cannot open file <%s>\n", filename );
PutStr( sbuf );
goto aio_return;
}
fseek( fp, 0l, 0 );
if ( newfile ) { /* New file */
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
strcpy( sbuf, "Enter number of pixels across" );
if ( wx == 1 ) strcat( sbuf, " for second grid:" );
else strcat( sbuf, ":" );
PutStr( sbuf );
sp = GetStr( );
PutStr("\n");
sscanf( sp, "%ld", &n_across[wx] );
if ( n_across[wx] < N_ACR_MIN ) n_across[wx] = N_ACR_MIN;
if ( n_across[wx] > N_ACR_MAX ) n_across[wx] = N_ACR_MAX;
PutStr( "Enter number of pixels down:" );
sp = GetStr( );
PutStr("\n");
sscanf( sp, "%ld", &n_down[wx] );
if ( n_down[wx] < N_DWN_MIN ) n_down[wx] = N_DWN_MIN;
if ( n_down[wx] > N_DWN_MAX ) n_down[wx] = N_DWN_MAX;
fprintf( fp,
"! number of pixels across = %2ld, number of pixels down = %2ld%s",
n_across[wx], n_down[wx], NEW_LINE_STR );
}
} else {
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
if ( fgets( fbuf, MAX_STR, fp ) == 0 ) goto aio_read_error;
sscanf( fbuf,
"! number of pixels across = %ld, number of pixels down = %ld",
&n_across[wx], &n_down[wx] );
if ( n_across[wx] < N_ACR_MIN || n_across[wx] > N_ACR_MAX ||
n_down[wx] < N_DWN_MIN || n_down[wx] > N_DWN_MAX ) {
PutStr( "Unexpected values in file\n" );
goto aio_return;
}
}
}
tot_n_inputs = 0;
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
n_inputs[wx] = n_across[wx] * n_down[wx];
tot_n_inputs += n_inputs[wx];
}
SetUpGrids( ); /* Set up grid parameters and grid window positions */
/* Set up menu and menu window */
AIOMenu.key = 3;
wxstrt[2] = ( xsize - AIOMenu.x1 ) / 2;
wxend[2] = wxstrt[2] + AIOMenu.x1 + 2;
wystrt[2] = ( wystrt[0] - AIOMenu.y1 ) / 2;
wyend[2] = wystrt[2] + AIOMenu.y1 + 2;
ug_window( AIOMenu.key, ip_grid[wx].gr_color[GR_OFF],
wxstrt[2], wystrt[2], wxend[2], wyend[2] );
n_patterns = FillPosArr( ); /* Fill Position array */
curr_pattern = n_patterns; /* End of file */
ReadFData( 0 ); /* Special eof case */
/* Display everything */
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
DispGrid( &ip_grid[wx] );
DispPattern( &ip_grid[wx] );
}
for (gmip = AIOMenu.item, wx = 0; wx < AIOMenu.num_items;
gmip++, wx++ )
gmip->flag &= ~GM_LOCKED;
DispGMenu( &AIOMenu );
for ( ; ; ) {
while ( ( gmip=LookGMenu(&AIOMenu,&button) ) != (GMENU_ITEM *) 0 ) {
switch( gmip->code ) {
case CR_SCROLL:
if ( button == MBUT_LEFT ) { /* Scroll down */
curr_pattern++;
if ( ReadFData( 0 ) < 0 ) goto aio_return;
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
DispPattern( &ip_grid[wx] );
}
if ( button == MBUT_RIGHT ) { /* Scroll up */
curr_pattern--;
if ( ReadFData( 0 ) < 0 ) goto aio_return;
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
DispPattern( &ip_grid[wx] );
}
break;
case CR_SEEK:
if ( button == MBUT_LEFT ) { /* Start of file data */
curr_pattern = 0;
if ( ReadFData( 0 ) < 0 ) goto aio_return;
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
DispPattern( &ip_grid[wx] );
}
if ( button == MBUT_RIGHT ) { /* End of file data */
curr_pattern = n_patterns;
if ( ReadFData( 0 ) < 0 ) goto aio_return;
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
DispPattern( &ip_grid[wx] );
}
break;
case CR_ENTER:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
WriteFData( 0 );
ReadFData( 0 );
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
DispPattern( &ip_grid[wx] );
}
break;
case CR_QUIT:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
IORTNCDE = -1;
goto aio_return;
}
break;
}
/* If button was pressed, wait for it to be released */
while ( button != 0 )
ug_mouse( &key, &xp, &yp, &button );
}
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
Sketch( &ip_grid[wx] );
}
aio_read_error:
sprintf( sbuf, "Error reading file <%s>\n", filename );
PutStr( sbuf );
aio_return:
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
for ( wx = 1; wx < 4; wx++ ) ug_windel( wx );
IORTNCDE = 1;
break;
case RQ_REWIND: /* rewind the input file */
if ( fp != (FILE *)0 ) fseek( fp, 0l, 0 );
break;
case RQ_LSTART: /* starting learn */
case RQ_RSTART: /* starting recall */
abortio = 0; /* abort flag */
autoip = 0; /* Assume hand input */
if ( fp == (FILE *)0 ) { /* Not yet opened */
sprintf( sbuf,
"Enter name of pattern file ( default: %s ):", filenroot );
PutStr( sbuf );
sp = GetStr( );
PutStr("\n");
if ( strcmp( sp, "" ) != 0 ) strncpy( filenroot, sp, 9 );
strcpy( filename, filenroot );
strcat( filename, ".nna" );
if ( ( fp = fopen( filename, "r" ) ) == (FILE *)0 ) {
sprintf( sbuf, "Cannot open file <%s>; learn aborted\n", filename );
PutStr( sbuf );
abortio = 1;
goto lrns_return;
}
shufflex = 0;
/* Initialize shuffle array */
for ( wx = 0; wx < MAX_N_PATTERNS; wx++ )
shuffle_array[ wx ] = wx;
n_patterns = FillPosArr( ); /* Fill Position array */
curr_pattern = 0; /* Start of of file */
}
fseek( fp, 0l, 0 ); /* rewind */
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
if ( fgets( fbuf, MAX_STR, fp ) == 0 ) {
PutStr( "Read error; learn aborted\n" );
abortio = 1;
goto lrns_return;
}
sscanf( fbuf,
"! number of pixels across = %ld, number of pixels down = %ld",
&n_across[wx], &n_down[wx] );
if ( n_across[wx] < N_ACR_MIN || n_across[wx] > N_ACR_MAX ||
n_down[wx] < N_DWN_MIN || n_down[wx] > N_DWN_MAX ) {
PutStr( "Unexpected values in file; learn aborted\n" );
abortio = 1; /* Abort */
goto lrns_return;
}
}
tot_n_inputs = 0;
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
n_inputs[wx] = n_across[wx] * n_down[wx];
tot_n_inputs += n_inputs[wx];
}
if ( tot_n_inputs != ninp ) {
PutStr( "Data incompatible with network; learn aborted\n" );
abortio = 1;
goto lrns_return;
}
SetUpGrids( ); /* Set up grid parameters and grid window positions */
/* Set up menu and menu window */
LrnMenu.key = 3;
wxstrt[2] = ( xsize - LrnMenu.x1 ) / 2;
wxend[2] = wxstrt[2] + LrnMenu.x1 + 2;
wystrt[2] = ( wystrt[0] - LrnMenu.y1 ) / 2;
wyend[2] = wystrt[2] + LrnMenu.y1 + 2;
ug_window( LrnMenu.key, gm_intcolor,
wxstrt[2], wystrt[2], wxend[2], wyend[2] );
lrns_return:
if ( abortio ) {
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
for ( wx = 1; wx < 4; wx++ ) ug_windel( wx );
}
break;
case RQ_LEARNIN: /* read training input */
case RQ_READ: /* read test data */
if ( abortio ) goto lrnin_return;
if ( IOLAYER != 0 ) {
PutStr( "This UserIO program expects data only at the input buffer.\n" );
PutStr( "You may be using an out-dated control strategy\n" );
abortio = 1;
goto lrnin_return;
}
if ( !autoip ) {
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
for (gmip = LrnMenu.item, wx = 0; wx < LrnMenu.num_items;
gmip++, wx++ )
gmip->flag &= ~GM_LOCKED;
ug_winclr( LrnMenu.key );
DispGMenu( &LrnMenu );
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
DispGrid( &ip_grid[wx] );
DispPattern( &ip_grid[wx] );
}
for ( ; ; ) {
while ( (gmip=LookGMenu(&LrnMenu,&button)) != (GMENU_ITEM *) 0 ) {
switch( gmip->code ) {
case CR_SCROLL:
if ( button == MBUT_LEFT ) { /* Scroll down */
curr_pattern++;
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
DispPattern( &ip_grid[wx] );
}
if ( button == MBUT_RIGHT ) { /* Scroll up */
curr_pattern--;
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
DispPattern( &ip_grid[wx] );
}
break;
case CR_SEEK:
if ( button == MBUT_LEFT ) { /* Start of file data */
curr_pattern = 0;
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
DispPattern( &ip_grid[wx] );
}
if ( button == MBUT_RIGHT ) { /* End of file data */
curr_pattern = n_patterns;
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
DispPattern( &ip_grid[wx] );
}
break;
case CR_ENTER:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
memcpy( IODATA, fdata, IOCOUNT*sizeof( float ) );
curr_pattern++;
goto lrnin_return;
}
break;
case CR_QUIT:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
IORTNCDE = -1;
goto lrnin_return;
}
break;
case CR_AUTO:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
autoip = 1;
goto auto_lrn;
}
break;
}
/* If button was pressed, wait for it to be released */
while ( button != 0 )
ug_mouse( &key, &xp, &yp, &button );
}
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
Sketch( &ip_grid[wx] );
}
} else { /* Auto input */
auto_lrn:
for (gmip = LrnMenu.item, wx = 0; wx < LrnMenu.num_items;
gmip++, wx++ )
gmip->flag &= ~GM_LOCKED;
ug_winclr( LrnMenu.key );
DispGMenu( &LrnMenu );
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
DispGrid( &ip_grid[wx] );
if ( shufflex == 0 ) Shuffle( n_patterns );
curr_pattern = shuffle_array[ shufflex++ ];
if ( ReadFData( 0 ) < 0 ) {
PutStr( "Error reading file; learn aborted\n" );
abortio = 1;
goto lrnin_return;
}
if ( shufflex >= n_patterns ) shufflex = 0;
memcpy( IODATA, fdata, IOCOUNT * sizeof( float ) );
for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
DispPattern( &ip_grid[wx] );
}
lrnin_return:
if ( abortio ) {
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
for ( wx = 1; wx < 4; wx++ ) ug_windel( wx );
IORTNCDE = -1;
}
break;
case RQ_WRSTEP: /* write interim results */
if ( abortio ) goto wrstep_return;
if ( BamFlag ) wx = ( IOLAYER == 1 ? 0 : 1 );
else wx = 0;
DispGrid( &ip_grid[wx] );
/* add size_t cast for MS-C 7.0; 4-28-93 mjs */
#ifdef MSC
memcpy( ip_grid[wx].gr_data, IODATA,
(size_t)(n_inputs[wx]*sizeof( float )) );
#else
memcpy( ip_grid[wx].gr_data, IODATA, n_inputs[wx]*sizeof( float ) );
#endif
DispPattern( &ip_grid[wx] );
if ( !autoip ) {
for ( ; ; ) {
PMenuList[CR_SCROLL].flag |= GM_LOCKED;
PMenuList[CR_SEEK].flag |= GM_LOCKED;
PMenuList[CR_AUTO].flag |= GM_LOCKED;
ug_winclr( LrnMenu.key );
DispGMenu( &LrnMenu );
PutStr(
"ENTER for next iteration, QUIT to end current recall \n");
for ( ; ; ) {
while ( (gmip=LookGMenu(&LrnMenu,&button))==(GMENU_ITEM *) 0) ;
switch( gmip->code ) {
case CR_ENTER:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
goto wrstep_return;
}
break;
case CR_QUIT:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
IORTNCDE = -1;
goto wrstep_return;
}
break;
}
}
}
}
wrstep_return:
if ( abortio ) {
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
for ( wx = 1; wx < 4; wx++ ) ug_windel( wx );
IORTNCDE = -1;
}
break;
case RQ_LEARNOUT: /* read desired output */
/* IODATA points to an empty array of IOCOUNT values. The
elements of the array will become the desired outputs for
training purposes. These desired outputs correspond to
the most recent "RQ_LEARNIN" request.
*/
case RQ_RCLTST: /* read desired output during recall test */
/* IODATA points to an empty array of IOCOUNT values. The
elements of the array will become the desired outputs for
recall purposes. This request is only made during a
Execute Network/Recall Test.
*/
break;
case RQ_LEARNRSLT:
case RQ_WRITE: /* write out results */
if ( abortio ) goto write_return;
if ( !autoip ) {
PutStr(
"Cycle complete; press left or right mouse button to continue.\n");
for ( ; ; ) {
ug_mouse( &key, &xp, &yp, &button ); /* wait for mouse */
if (button == MBUT_LEFT || button == MBUT_RIGHT) break;
}
}
write_return:
if ( abortio ) {
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
for ( wx = 1; wx < 4; wx++ ) ug_windel( wx );
IORTNCDE = -1;
}
break;
case RQ_TERM: /* terminate interface */
/* close any files which may be open. Deallocate any memory
which was allocated. (This is VERY VERY important. If
allocated memory is NOT released, dos memory will become
fragmented and it will become necessary to reboot.
*/
#if 1
free((void *)filepos);
free((void *)shuffle_array);
free((void *)fdata);
#else
free(filepos);
free(shuffle_array);
free(fdata);
#endif
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
break;
}
return;
}